package cn.echo.modules.sys.service;

import cn.echo.modules.sys.dao.CommentMapper;
import cn.echo.modules.sys.entity.Comment;
import cn.echo.modules.sys.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created by 郭鹏 on 2017/4/13.
 */
public class CommentService {

    //查看所有评论
    public List<Comment> findAll(Comment comment) {
        SqlSession sqlSession = null;
        sqlSession = MyBatisUtils.getSqlFactory().openSession(true);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        List<Comment> commentList = commentMapper.findAll(new Comment());
        sqlSession.close();
        return  commentList;
    }

    //插入一条数据
    public boolean insertComment(Comment comment){
        SqlSession sqlSession = null;
        sqlSession = MyBatisUtils.getSqlFactory().openSession(true);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        return commentMapper.insertComment(comment);
    }

    //删除一条评论（假删除）
    public boolean updateCommentById(Comment comment){
        boolean b = false;
        SqlSession sqlSession = null;
        sqlSession = MyBatisUtils.getSqlFactory().openSession(true);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        try {
           b = commentMapper.updateCommentById(comment);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return b;
    }

    //删除一条数据（真删除）
    public boolean deleteCommentById(Comment comment){
        boolean b = false;
        SqlSession sqlSession = null;
        sqlSession = MyBatisUtils.getSqlFactory().openSession(true);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        try {
            b = commentMapper.deleteCommentById(comment);
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return b;
    }

}
